import { createRouter, createWebHashHistory } from 'vue-router'
import { useAuthStore } from '../stores/auth'
import { useSidebarStore } from '../stores/sidebar'

const routes = [
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/auth/Login.vue'),
        meta: { requiresAuth: false }
    },
    {
        path: '/',
        name: 'Home',
        component: () => import('../views/home/Home.vue'),
        meta: { requiresAuth: true },
        children: [
            {
                path: '',
                name: 'HomeRedirect',
                redirect: '/media/text-gen',
            },
            {
                path: 'profile',
                name: 'Profile',
                component: () => import('../views/profile/index.vue'),
                children: [
                    {
                        path: 'account',
                        name: 'Account',
                        component: () => import('../views/profile/Account/index.vue'),
                    },
                    {
                        path: 'password',
                        name: 'Password',
                        component: () => import('../views/profile/MyPassword/index.vue'),
                        meta: { parentRoute: '/profile/account' }
                    }
                ]
            },
            {
                path: 'media',
                name: 'Media',
                component: () => import('../views/media/index.vue'),
                children: [
                    {
                        path: 'text-gen',
                        name: 'TextGen',
                        component: () => import('../views/media/TextGen/index.vue'),
                        meta: { generationType: 0, keepAlive: true }
                    },
                    {
                        path: 'image-gen',
                        name: 'ImageGen',
                        component: () => import('../views/media/ImageGen/index.vue'),
                        meta: { generationType: 1, keepAlive: true }
                    },
                    {
                        path: 'copy-opt',
                        name: 'CopyOpt',
                        component: () => import('../views/media/CopyOpt/index.vue'),
                        meta: { generationType: 2, keepAlive: true }
                    },
                    {
                        path: 'note',
                        name: 'Note',
                        component: () => import('../views/media/Note/index.vue'),
                        meta: { keepAlive: false }
                    },
                    {
                        path: 'stats',
                        name: 'Stats',
                        component: () => import('../views/media/Stats/index.vue'),
                        meta: { keepAlive: false }
                    }
                ]
            },
            {
                path: 'consulting',
                name: 'Consulting',
                component: () => import('../views/consulting/index.vue'),
                children: [
                    {
                        path: 'customer',
                        name: 'CustomerList',
                        component: () => import('../views/consulting/Customer/list.vue')
                    },
                    {
                        path: 'detail',
                        name: 'CustomerDetail',
                        component: () => import('../views/consulting/Customer/Detail.vue'),
                        meta: { parentRoute: '/consulting/customer' }
                    },
                    {
                        path: 'ai-trainer-registration',
                        name: 'AiTrainerRegistration',
                        component: () => import('../views/consulting/Customer/AiTrainerRegistration.vue'),
                        meta: { parentRoute: '/consulting/customer' }
                    },
                    {
                        path: 'class',
                        name: 'ClassList',
                        component: () => import('../views/consulting/class/index.vue')
                    },
                    {
                        path: 'class-detail',
                        name: 'ClassDetail',
                        component: () => import('../views/consulting/class/detail.vue'),
                        meta: { parentRoute: '/consulting/class' }
                    },
                    {
                        path: 'note-code',
                        name: 'NoteCodeList',
                        component: () => import('../views/consulting/Note/index.vue')
                    }
                ]
            },
            {
                path: 'xueguan',
                name: 'xueguan',
                component: () => import('../views/xueguan/index.vue'),
                children: [
                    {
                        path: 'class',
                        name: 'XueguanClassList',
                        component: () => import('../views/xueguan/class/index.vue')
                    },
                    {
                        path: 'detail',
                        name: 'xueguanClassDetail',
                        component: () => import('../views/xueguan/class/detail.vue'),
                        meta: { parentRoute: '/xueguan/class' }
                    }
                ]
            },
            {
                path: 'jiaoxue',
                name: 'jiaoxue',
                component: () => import('../views/jiaoxue/index.vue'),
                children: [
                    {
                        path: 'class',
                        name: 'JiaoxueClassList',
                        component: () => import('../views/jiaoxue/class/index.vue')
                    },
                    {
                        path: 'detail',
                        name: 'JiaoxueClassDetail',
                        component: () => import('../views/jiaoxue/class/detail.vue'),
                        meta: { parentRoute: '/jiaoxue/class' }
                    },
                    {
                        path: 'schedule',
                        name: 'JiaoxueClassSchedule',
                        component: () => import('../views/jiaoxue/class/Schedule.vue'),
                        meta: { parentRoute: '/jiaoxue/class' }
                    },
                ]
            }
        ]
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    const authStore = useAuthStore()
    const sidebarStore = useSidebarStore()

    if (to.meta.requiresAuth && !authStore.token) {
        next({ name: 'Login', query: { redirect: to.fullPath } })
        return
    }
    if (to.name === 'Login' && authStore.token) {
        next({ name: 'Home' })
        return
    }

    if (!sidebarStore.initialized) {
        sidebarStore.restoreState()
        sidebarStore.initialized = true
    }
    sidebarStore.syncStateFromPath(to.path)

    if (to.meta.generationType !== undefined) {
        sidebarStore.setGenerationType(to.meta.generationType)
    }

    next()
})

export default router